import { toLeadExcle } from "@/views/client-manage/api/clientlessee";
import { toLeadProviderExcle } from "@/views/client-manage/api/clientprovider";

export default {
	name: "leading-in-first",

	mounted() {},
	data() {
		return {
			// hxf-0806-上传组件使用-----开始--------------
			fileSize: 50,
			url: "/api/clientlessee/toparseexcel",
			fileList: []
		};
	},
	props: {
		clientType: {
			type: String
		}
	},
	methods: {
		downTemplate() {
			const { origin, pathname } = window.location;
			let url = `${origin}/${pathname.split("/")[1]}`;
			if (this.clientType) {
				window.location.href = url + "/clientprovider.xlsx";
			} else {
				window.location.href = url + "/clientlessee.xlsx";
			}
		},
		matchType(fileName) {
			// 后缀获取
			var suffix = "";
			// 获取类型结果
			var result = "";
			try {
				var flieArr = fileName.split(".");
				suffix = flieArr[flieArr.length - 1];
			} catch (err) {
				suffix = "";
			}
			// fileName无后缀返回 false
			if (!suffix) {
				result = false;
				return result;
			}
			// 图片格式
			var imglist = ["png", "jpg", "jpeg", "bmp", "gif"];
			// 进行图片匹配
			result = imglist.some(function(item) {
				return item == suffix;
			});
			if (result) {
				result = "image";
				return result;
			}
			// 匹配txt
			var txtlist = ["txt"];
			result = txtlist.some(function(item) {
				return item == suffix;
			});
			if (result) {
				result = "txt";
				return result;
			}
			// 匹配 excel
			var excelist = ["xls", "xlsx"];
			result = excelist.some(function(item) {
				return item == suffix;
			});
			if (result) {
				result = "excel";
				return result;
			}
			// 匹配 word
			var wordlist = ["doc", "docx"];
			result = wordlist.some(function(item) {
				return item == suffix;
			});
			if (result) {
				result = "word";
				return result;
			}
			// 匹配 pdf
			var pdflist = ["pdf"];
			result = pdflist.some(function(item) {
				return item == suffix;
			});
			if (result) {
				result = "pdf";
				return result;
			}
			// 匹配 ppt
			var pptlist = ["ppt"];
			result = pptlist.some(function(item) {
				return item == suffix;
			});
			if (result) {
				result = "ppt";
				return result;
			}
			// 匹配 视频
			var videolist = ["mp4", "m2v", "mkv"];
			result = videolist.some(function(item) {
				return item == suffix;
			});
			if (result) {
				result = "video";
				return result;
			}
			// 匹配 音频
			var radiolist = ["mp3", "wav", "wmv"];
			result = radiolist.some(function(item) {
				return item == suffix;
			});
			if (result) {
				result = "radio";
				return result;
			}
			// 其他 文件类型
			result = "other";
			return result;
		},
		uploadSectionFile(param) {
			//file就是当前添加的文件
			console.log(param.file, "param.file");
			this.fileList = param.file;
			// FormData 对象
			var form = new FormData();
			// 文件对象,key是后台接受的参数名称
			form.append("multipartFile", this.fileList);
			if (this.clientType) {
				toLeadProviderExcle(form).then(res => {
					// 这里做上传后的操作
					this.$message.success("上传成功");
					this.$emit("stepNext", res.data.data);
					console.log(res.data.data, "leading-in-first");
				});
			} else {
				toLeadExcle(form).then(res => {
					// 这里做上传后的操作
					this.$message.success("上传成功");
					this.$emit("stepNext", res.data.data);
					console.log(res.data.data, "leading-in-first");
				});
			}
		},
		next() {
			if (this.fileList.length > 0) {
				this.$refs.upload.submit();
			} else {
				this.$message.error("请选择上传文件!");
			}
		},
		getFile(event) {
			console.log(event);
			this.fileList = event.target.files[0];
			console.log(this.fileList, "this.fileList");
		},
		handleRemove(file, fileList) {
			console.log(file, fileList);
		},
		handlePreview(file) {},
		handleExceed(files, fileList) {
			this.$message.warning(
				`当前限制选择 1 个文件，本次选择了 ${
					files.length
				} 个文件，共选择了 ${files.length + fileList.length} 个文件`
			);
		},
		beforeRemove(file, fileList) {
			return this.$confirm(`确定移除 ${file.name}？`);
		},
		handleChange(file, fileList) {
			console.log(file, "file.handleChange");
			this.fileList = fileList;
		},
		// hxf-0806-上传组件使用-----开始--------------
		// hxf-0801-上传成功
		onSuccess(response, file, fileList) {
			if (response.data.code == 0) {
				console.log(response.data, "response.data");
			} else {
				console.log(response.data.msg, "response.data.msg");
			}
		},
		// hxf-0801-上传失败
		onError(err, file, fileList) {
			//this.loadClose()
			this.err("上传附件失败" + err);
		},
		// hxf-0801-上传前
		beforeUpload(file) {
			const isXLSX = this.matchType(file.name) === "excel";
			if (!isXLSX) {
				this.$message.error("文件格式只能是xlsx/xls!");
			}
			return isXLSX;
		}
	}
};
